package com.doit.day02;

import org.apache.kafka.clients.admin.*;
import java.util.Arrays;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutionException;

public class _01_对topic的操作 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        Properties props = new Properties();
        props.setProperty(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG,"linux01:9092");
        props.setProperty(AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG,"3000");
        AdminClient admin = KafkaAdminClient.create(props);

        //create
/*
        NewTopic topicFromJava = new NewTopic("topicFromJava2", 3, (short) 2);
        CreateTopicsResult topics1 = admin.createTopics(Arrays.asList(topicFromJava));
*/

       /* try {
            topics1.all().get();
        } catch (Exception e) {
            System.out.println("有异常了，异常是："+e);
        }
        System.out.println("能执行到这边代表topic已经创建了");*/

/*        //list
        ListTopicsResult topics = admin.listTopics();
        KafkaFuture<Set<String>> names = topics.names();
        Set<String> set = names.get();
        for (String s : set) {
            System.out.println(s);
        }*/


/*
        DeleteTopicsResult deleteTopicsResult = admin.deleteTopics(Arrays.asList("topicFromJava2", "topicFromJava1", "topicFromJava"));
        try {
            deleteTopicsResult.all().get();
        } catch (Exception e) {
            System.out.println("有异常了，异常是："+e);
        }
*/

        DescribeTopicsResult doit40 = admin.describeTopics(Arrays.asList("doit40"));
        Map<String, TopicDescription> map = doit40.all().get();
        Set<Map.Entry<String, TopicDescription>> entry = map.entrySet();
        for (Map.Entry<String, TopicDescription> ent : entry) {
            String key = ent.getKey();
            TopicDescription value = ent.getValue();
            System.out.println(key +","+value);
        }




    }
}
